﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.DAL;
using MINERP.MODEL.FoundationModel;

namespace MINERP.BLL.PrintManage
{
    public class PaperWasteTypeBLL
    {
        /// <summary>
        /// 获取纸浪费类型列表
        /// </summary>
        /// <returns></returns>
        public List<PaperWasteType_info> GetPaperWasteTypeInfoList()
        {
            List<PaperWasteType_info> list = new List<PaperWasteType_info>();
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                List<MINERP.DAL.PaperWasteType> paperWasteTypeList = entities.PaperWasteType.ToList();
                foreach (var item in paperWasteTypeList)
                {
                    PaperWasteType_info pWasteTypeInfo = new PaperWasteType_info();
                    pWasteTypeInfo.Id = item.Id;
                    pWasteTypeInfo.PaperOperationTypeName = item.PaperOperationTypeName;
                    list.Add(pWasteTypeInfo);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取纸浪费类型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public PaperWasteType_info GetPaperWasteTypeInfo(string name)
        {
            PaperWasteType_info pWasteInfo = new PaperWasteType_info();
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {

                MINERP.DAL.PaperWasteType pwastetype = entities.PaperWasteType.FirstOrDefault(p => p.PaperOperationTypeName == name);
                if (pwastetype == null)
                {
                    int ret = Insert(name);
                    pwastetype = entities.PaperWasteType.FirstOrDefault(p => p.PaperOperationTypeName == name);
                    if (ret == -1)
                    {
                        return null;
                    }
                }


                pWasteInfo.Id = pwastetype.Id;
                pWasteInfo.PaperOperationTypeName = pwastetype.PaperOperationTypeName;

            }
            return pWasteInfo;
        }

        /// <summary>
        /// 插入纸浪费类型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int Insert(string name)
        {
            int ret = 0;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                try
                {
                    MINERP.DAL.PaperWasteType pwasteType = new MINERP.DAL.PaperWasteType();
                    pwasteType.PaperOperationTypeName = name;
                    entities.AddToPaperWasteType(pwasteType);
                    entities.SaveChanges();
                }
                catch
                {
                    ret = -1;
                }
            }
            return ret;
        }
    }
}
